package org.dreamwork.assistant.tools.network.gui;

/**
 * Created by seth.yang on 2018/5/7
 */
public class Utils {
    public static byte[] toHex (String text) {
        text = text.replaceAll ("\\s+", "");
        if (text.length () % 2 != 0) {
            text = "0" + text;
        }
        char[] chs = text.toLowerCase ().toCharArray ();
        byte[] buff = new byte[chs.length / 2];
        int pos = 0, n, ch;
        for (int i = 0; i < chs.length; i += 2) {
            ch = chs[i];
            if (ch >= '0' && ch <= '9') {
                n = ch - '0';
            } else if (ch >= 'a' && ch <= 'f') {
                n = ch - 'a' + 10;
            } else {
                throw new IllegalArgumentException ("Invalid data");
            }
            n <<= 4;
            ch = chs[i + 1];
            if (ch >= '0' && ch <= '9') {
                n |= ch - '0';
            } else if (ch >= 'a' && ch <= 'f') {
                n |= ch - 'a' + 10;
            }
            buff[pos++] = (byte) n;
        }
        return buff;
    }
}
